package com.zone.domain.strategy.service.raffle.impl;

import com.zone.domain.acticity.model.entity.ActivityEntity;
import com.zone.domain.acticity.repository.IActivityRepository;
import com.zone.domain.strategy.model.entity.StrategyAwardEntity;
import com.zone.domain.strategy.model.entity.StrategyEntity;
import com.zone.domain.strategy.model.entity.StrategyRuleEntity;
import com.zone.domain.strategy.model.valobj.RuleTreeVO;
import com.zone.domain.strategy.repository.IStrategyRepository;
import com.zone.domain.strategy.service.IRaffleAward;
import com.zone.domain.strategy.service.rule.chain.RaffleRuleFilterChain;
import com.zone.domain.strategy.service.rule.tree.factory.DefaultTreeNodeFactory;
import com.zone.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.Validate;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author: zongzi
 * @description: 抽奖实现
 * @date: 2024/6/3
 */
@Slf4j
@Service
public class DefaultRaffleStrategy extends AbstractRaffleStrategy implements IRaffleAward {
    @Autowired
    private RaffleRuleFilterChain raffleRuleFilterChain;
    @Autowired
    private IStrategyRepository strategyRepository;
    @Autowired
    private DefaultTreeNodeFactory treeNodeFactory;
    @Autowired
    private IActivityRepository activityRepository;

    @Override
    public RaffleRuleFilterChain.StrategyAwardVO executeFilterChainBeforeRaffle(Long strategyId, String userId) {
        StrategyEntity strategyEntity = strategyRepository.getStrategyEntity(strategyId);
        Validate.notNull(strategyEntity, "未知的抽奖活动！请确认");
        return raffleRuleFilterChain.doFilter(strategyId, userId, strategyEntity.parseRuleModels());
    }

    @Override
    public DefaultTreeNodeFactory.StrategyAwardVO executeRuleTreeRaffle(Long strategyId, String userId, Integer awardId) {
        // 获取奖品策略配置
        StrategyAwardEntity strategyAwardEntity = strategyRepository.getStrategyAward(strategyId, awardId);
        DefaultTreeNodeFactory.StrategyAwardVO allow = DefaultTreeNodeFactory.StrategyAwardVO.builder().awardId(awardId).build();
        if (Objects.isNull(strategyAwardEntity) || Strings.isEmpty(strategyAwardEntity.getRuleModels())) {
            return allow;
        }
        // 查询规则树id
        StrategyRuleEntity strategyRuleEntity = strategyRepository.queryStrategyAwardRule(strategyId, awardId);
        String treeId = strategyRuleEntity.getRuleValue();
        RuleTreeVO ruleTreeVO = strategyRepository.getRuleTreeVoByTreeId(treeId);
        if (Objects.isNull(ruleTreeVO)) {
            return allow;
        }
        IDecisionTreeEngine decisionTreeEngine = treeNodeFactory.openLogic(ruleTreeVO);
        return decisionTreeEngine.execute(strategyId, userId, awardId);
    }

    @Override
    public List<StrategyAwardEntity> queryStrategyAwardList(Long strategyId) {
        return strategyRepository.listStrategyAward(strategyId);
    }

    @Override
    public List<StrategyAwardEntity> queryStrategyAwardListByActivityId(Long activityId) {
        ActivityEntity activityEntity = activityRepository.queryActivity(activityId);
        return this.queryStrategyAwardList(activityEntity.getStrategyId());
    }
}
